package com.xique.park.controller.client.web.service.biz.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.park.bean.domain.ParkCar;
import com.xique.park.bean.domain.ParkDeviceNew;
import com.xique.park.controller.client.web.api.IWxClientCommandApi;
import com.xique.park.controller.client.web.service.biz.service.IWxClientCommandService;
import com.xique.park.service.service.IParkDeviceNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 微信命令下发commandService
 */
@Service
public class WxClientCommandServiceImpl implements IWxClientCommandService {

    /**下载车辆*/
    String CommandCarDownload = "service.park.command.down_car";

    /**删除车辆*/
    String CommandCarDel = "service.park.command.del_car";

    String DATA = "data";

    String REC_IDS = "rec_ids";

    String LIST = "list";

    String VER_1_0 = "1.0";

    String VER_2_0 = "2.0";

    String VER_3_0 = "3.0";

    @Autowired
    private IWxClientCommandApi wxClientCommandApi;

    @Autowired
    private IParkDeviceNewService parkDeviceNewService;

    private JSONObject getBaseObj(Long parkId, String sn, String serviceName, String version){
        JSONObject obj = new JSONObject();
//        ParkApiConfig parkApiConfig = getParkApiConfig(parkId);
        obj.put("mqtt_key","");
        obj.put("charset","UTF-8");
        obj.put("service",serviceName);
        obj.put("version",version);
        obj.put("sign","123456789456");
        obj.put("ts",System.currentTimeMillis()/1000);
        obj.put("device_code",sn);
        return obj;
    }

    @Override
    public List<String> getMainDeviceCodesByParkId(Long parkId) {
        ParkDeviceNew parkDeviceNew = new ParkDeviceNew();
        parkDeviceNew.setParkId(parkId);
        parkDeviceNew.setStatus("normal");
        parkDeviceNew.setIsMain("Y");
        List<ParkDeviceNew> parkDeviceNewList = parkDeviceNewService.selectParkDeviceNewList(parkDeviceNew);
        List<String> devCodes = new ArrayList<>();
        if(!CollectionUtils.isEmpty(parkDeviceNewList)){
            devCodes = parkDeviceNewList.stream().map(ParkDeviceNew::getCode).collect(Collectors.toList());
        }
        return devCodes;
    }

    /**
     * 车场所有设备下载单个车辆信息
     * @param parkId
     * @param parkCar
     * @return
     */
    @Override
    public AjaxResult downSingleCar(Long parkId, ParkCar parkCar) {
        List<String> devCodes = getMainDeviceCodesByParkId(parkId);
        if(!CollectionUtils.isEmpty(devCodes)){
            Map<String,Object> resMap = new HashMap<>(devCodes.size());
            for(String devCode : devCodes){
                resMap.put(devCode,downSingleCar(devCode,parkCar));
            }
            return AjaxResult.success(resMap);
        }else{
            return AjaxResult.success("成功");
        }
    }

    @Override
    public AjaxResult downSingleCar(List<String> devCodes, ParkCar parkCar) {
        return null;
    }

    /**
     * 单个设备下载单个车辆信息
     * @param sn
     * @param parkCar
     * @return
     */
    @Override
    public AjaxResult downSingleCar(String sn, ParkCar parkCar) {
        JSONObject obj = getBaseObj(parkCar.getParkId(),sn,CommandCarDownload,VER_2_0);
        JSONObject dataObj = new JSONObject();
        List<ParkCar> parkCarList = new ArrayList<>(1);
        parkCarList.add(parkCar);
        dataObj.put(LIST,parkCarList);
        List<Long> recIdList = parkCarList.stream().map(u -> u.getId()).collect(Collectors.toList());
        dataObj.put(REC_IDS,recIdList);
        obj.put(DATA, dataObj);
        Map<String, Object> params = JSONObject.toJavaObject(obj, Map.class);
        AjaxResult result = wxClientCommandApi.downSingleCar(params);
        return result;
    }

    @Override
    public AjaxResult delSingleCar(Long parkId, ParkCar parkCar) {
        List<String> devCodes = getMainDeviceCodesByParkId(parkId);
        if(!CollectionUtils.isEmpty(devCodes)){
            Map<String,Object> resMap = new HashMap<>(devCodes.size());
            for(String devCode : devCodes){
                resMap.put(devCode,delSingleCar(devCode,parkCar));
            }
            return AjaxResult.success(resMap);
        }else{
            return AjaxResult.success("成功");
        }
    }

    @Override
    public AjaxResult delSingleCar(String sn, ParkCar parkCar) {
        JSONObject obj = getBaseObj(parkCar.getParkId(),sn,CommandCarDel,VER_1_0);
        List<String> plateList = new ArrayList<>(1);
        plateList.add(parkCar.getPlate());
        obj.put(DATA, JSONArray.toJSON(plateList));
        Map<String, Object> params = JSONObject.toJavaObject(obj, Map.class);
        AjaxResult result = wxClientCommandApi.delSingleCar(params);
        return result;
    }
}
